package com.qffx.moviebase.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by lx on 2017/2/9.
 */

public class NetWorkReceiver extends BroadcastReceiver {
    private ConnectivityManager mConnectivityManager;
    private NetworkInfo netInfo;
    private ICallBack callback;

    public interface ICallBack {
        void getState(Boolean b);
    }

    public NetWorkReceiver(ICallBack callback) {
        this.callback = callback;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {

            mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            netInfo = mConnectivityManager.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isAvailable()) {

                /////////////网络连接
                String name = netInfo.getTypeName();

                if (netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    /////WiFi网络
                  //  Toast.makeText(context, "网络连接", Toast.LENGTH_SHORT).show();

                } else if (netInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
                    /////有线网络
                   // Toast.makeText(context, "网络连接", Toast.LENGTH_SHORT).show();

                } else if (netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                    /////////3g网络
                 //   Toast.makeText(context, "网络连接", Toast.LENGTH_SHORT).show();

                }
            } else {
                callback.getState(true);
            }
        }

    }

}
